using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Xfinity.Weapons;

namespace Xfinity.Loaders
{
    
    public static class WeaponSystem
    {

        private static KeyedCollection<string,RangedWeapon> rangedWeapons;
        private static bool rangedWeaponsBeenSet;
        [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public static KeyedCollection<string,RangedWeapon> RangedWeapons
        {
            get { return rangedWeapons; }
            set { if (!rangedWeaponsBeenSet) { rangedWeapons = value; rangedWeaponsBeenSet = true; } }
        }



        public static void LoadWeapons(string path)
        {
            //string filenames[] = Path.
        }
        
        
        
        
        static XmlSerializer serializer = new XmlSerializer(typeof(WeaponProperties));
        
        public static void Serialize(WeaponProperties weapon, string filename)
        {
            
            using (XmlTextWriter s = new XmlTextWriter(filename,Encoding.UTF8))
            {
                s.Formatting = Formatting.Indented;
                s.WriteStartElement("XnaContent");
                s.WriteStartElement("Asset");
                s.WriteAttributeString("Type", weapon.GetType().Name);
                s.WriteEndElement();
                serializer.Serialize(s, weapon);
                s.WriteEndElement();
            }
            
        }

        public static T Deserialize<T>(string filename)
        {
            using (XmlTextReader s = new XmlTextReader(new FileStream(filename, FileMode.Open)))
            {
                serializer = new XmlSerializer(typeof(T));
                s.ReadStartElement();
                s.ReadStartElement("Asset");
                return (T)serializer.Deserialize(s.ReadSubtree());
            }
            
        }

    }
}
